#include <functional>
#include <string>
#include <cstdio>
#include <iostream>
using namespace std;

const string opArr = "+-*/%";

class CalTask
{
public:
    using func_t = function<int(int, int, char)>;

public:
    CalTask()
    {
    }

    CalTask(int x, int y, char op, func_t func)
        : _x(x), _y(y), _op(op), _callfunc(func)
    {
    }

    // 仿函数
    string operator()()
    {
        int res = _callfunc(_x, _y, _op);
        char buffer[1024];
        snprintf(buffer, sizeof buffer, "%d %c %d = %d", _x, _op, _y, res);
        return buffer;
    }

    // 在生产者这里拿到生产的数据
    string toTaskString()
    {
        char buffer[1024];
        snprintf(buffer, sizeof buffer, "%d %c %d = ?", _x, _op, _y);
        return buffer;
    }

private:
    int _x;
    int _y;
    func_t _callfunc;
    char _op;
};

int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;
    case '/':
    {
        if (y == 0)
        {
            cout << "div zero error!" << endl;
            return -1;
        }
        else
        {
            result = x / y;
        }
    }
    break;
    case '%':
    {
        if (y == 0)
        {
            cout << "mod zero error!" << endl;
            return -1;
        }
        else
        {
            result = x % y;
        }
    }
    break;
    default:
        // do nothing
        break;
    }

    return result;
}

class SaveTask
{
public:
    typedef function<void(const string &)> func_t;

public:
    SaveTask()
    {
    }

    SaveTask(const string &message, func_t func)
        : _message(message), _func(func)
    {
    }

    void operator()()
    {
        _func(_message);
    }

private:
    string _message; // 任务名称
    func_t _func;    // 执行的方法
};

// 执行的方法
void Save(const string &message)
{
    const char *filename = "./log.txt";
    FILE *fp = fopen(filename, "w+");
    if (!fp)
    {
        cout << "open file fail! " << endl;
    }
    fputs(message.c_str(), fp);
    fputs("\n", fp);
    fclose(fp);
}